package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

public class RegDemo {
    public static void main(String[] args) throws Exception {
//        Scanner sc = new Scanner(System.in);
//        System.out.println("请输入用户名：");
//        String username = sc.nextLine();
//        System.out.println("请输入密码：");
//        String password = sc.nextLine();
//        System.out.println("请输入昵称：");
//        String nickname = sc.nextLine();
//        System.out.println("请输入年龄：");
//        int age = sc.nextInt();
//
//        User user = new User();
//        user.setUsername(username);
//        user.setPassword(password);
//        user.setNickname(nickname);
//        user.setAge(age);
        User user = InputUtil.getInputObject(new  User(),"用户注册");
        insert(user);
    }

    public static void insert(User user) throws Exception {
        String sql = "INSERT INTO user(username,password,nickname,age) " +
                "VALUES('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getNickname()+"',"+user.getAge()+")";
        System.out.println(sql);
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3307/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root");
        Statement statement = connection.createStatement();
        int num = statement.executeUpdate(sql);
        System.out.println(num > 0 ? "注册成功" : "注册失败");
        connection.close();

    }
}
